﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.Base;
using log4net;
using Engine.Input.Handling;
using Engine.Messaging;
using Engine.Messaging.Messages;
using Engine.Input.Handling.KeyboardHandling;

namespace Engine.Input
{
    public class InputHandlerSystem : ISubSystem
    {
        #region Properties
        public List<IInputHandler> Handles { get; set; }
        private MessageSystem MessageSystem { get; set; }
        #endregion // Properties


        #region Constructors
        public InputHandlerSystem(MessageSystem messageSystem)
        {
            this.MessageSystem = messageSystem;
            Handles = new List<IInputHandler>();
            Handles.Add(new KeyboardHandler());
        }
        #endregion // Constructors


        #region Methods
        public void ProcessInput()
        {
            Handles.ForEach(handle => handle.SampleInput());
        }

        internal void BroadcastMessage<T>(IInputHandler sender, T m) where T : Message
        {
            MessageSystem.Broadcast(sender, m);
        }
        #endregion // Methods


        #region ISubSystem-Implementation
        public void Start()
        {
            Logger.Debug("Starting InputHandlerSystem");
            Handles.ForEach(handle => handle.System = this);
        }

        public void Stop()
        {
            Logger.Debug("Stoping InputHandlerSystem");
            Handles.ForEach(handle => handle.Detach());
            Handles.Clear();
        }
        #endregion // ISubSystem-Implementation


        #region STATIC
        private static ILog Logger { get; set; }
        static InputHandlerSystem()
        {
            Logger = LogManager.GetLogger(typeof(InputHandlerSystem));
        }
        #endregion // STATIC
    }
}
